<?php
/**
 * 广告中心逻辑处理
 * User：LLW
 * Date:2020-03-18
 */

namespace app\caidongweb\model;

class AdverModel extends BaseModel
{
    protected $table = 'cd_adver';

    /**
     * 首页信息
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $where = [];
        input('key') ? $where['ae_name'] = array('like',"%".input('key')."%") : false;
        $list = $this->where($where)->where('ae_state','neq',2)->order('ae_sort asc')->paginate(20,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 广告回收处
     * @return \think\Paginator
     */
    public function getBin()
    {
        $where = [];
        $start = strtotime(input('start'));
        $end = strtotime(input('end'));
        if($start && $end){
            $where['ae_time'] = array('between',"$start,$end");
        }
        input('key') ? $where['ae_name'] = array('like',"%".input('key')."%") : false;
        $list = $this->where($where)->where('ae_state',2)->order('ae_time desc')->paginate(20,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 获取单个数据
     * @return array|false|\PDOStatement|string|
     */
    public function getFind()
    {
        $id = input('id',null);
        $find = $this->where('ae_id',$id)->find();
        return $find;
    }

    /**
     * 轮播广告编辑添加
     * @return array
     */
    public function getAddEdit()
    {
        $data = input('post.');
        if(!empty($data['headImg'])){
            $data['ae_img'] = $data['headImg'];
        }
        unset($data['headImg']);
        if(!empty($data['ae_id'])){
            $res = $this->where('ae_id',$data['ae_id'])->update($data);
        }else{
            unset($data['ae_id']);
            $data['ae_time'] = time();
            $res = $this->insert($data);
        }
        if($res){
            return $this->returnData(1,'成功');
        }else{
            return $this->returnData(-1,'失败');
        }
    }
}